<?php

// +----------------------------------------------------------------------
// | 第三方存储
// +----------------------------------------------------------------------
namespace app\attachment\controller;

use app\common\controller\Adminbase;
use think\Db;
use app\admin\model\Config as Config_Model;
use app\attachment\model\ThirdOss as ThirdOss_Model;

class ThirdOss extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {

            $list = Db::name('third_oss')->field('id, key, value, status')->select();
            $result = array("code" => 0, "data" => $list);
            return json($result);
        }

        return $this->fetch('thirdOss/index');
    }

    public function edit()
    {
        $id = $this->request->param('id/d');
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['status'] = 1;
            $res = Db::name('third_oss')->where('id', $data['id'])->update($data);
            if ($res) {
                $result = ['code' => 0, 'msg' => '操作成功'];
            } else {
                $result = ['code' => -1, 'msg' => '操作失败'];
            }
            return json($result);
        } else {
            $info = Db::name('third_oss')->findOrEmpty($id);
            $this->assign('info', $info);
            return $this->fetch('thirdOss/form');
        }
    }

    /**
     * 第三方存储配置
     * @param string $name
     * @return mixed|null
     */
    public function show(string $name = 'aliyuncs')
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['key'] = $name;
            $data['status'] = 1;
            switch ($name) {
                case 'aliyuncs':
                    $data['value'] = '阿里云存储';
                    break;
                case 'tencent':
                    $data['value'] = '腾讯云存储';
                    break;
                case 'douyin':
                    $data['value'] = '抖音云存储';
                    break;
            }
            $info = ThirdOss_Model::where('key', $name)->find();
            if ($info) {
                $res = $info->allowField(true)->save($data);
            } else {
                $res = ThirdOss_Model::create($data);
            }
            if ($res !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        } else {
            $upload_config = Config_Model::where('name', 'upload_driver')
                ->value('options');
            $type_arr = parse_attr($upload_config);
            array_shift($type_arr); //删除本地选项

            $info = ThirdOss_Model::where('key', $name)->find();
            $this->assign([
                'nameArray' => $type_arr,
                'info' => $info ?: [],
                'name' => $name,
            ]);
            return $this->fetch('thirdOss/show');
        }
    }

}
